<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Swagger Login</title>
    <link rel="stylesheet" href="../druid/css/bootstrap.min.css" />
    <script type="text/javascript" src="../druid/js/jquery.min.js"></script>
    <style type="text/css">
        html, body {
            background-color: #eee;
        }
        body {
            padding-top: 40px; 
        }
        .container {
            width: 300px;
        }

        .container > .content {
            background-color: #fff;
            padding: 20px;
            margin: 0 -20px; 
            -webkit-border-radius: 10px 10px 10px 10px;
               -moz-border-radius: 10px 10px 10px 10px;
                    border-radius: 10px 10px 10px 10px;
            -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
               -moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
                    box-shadow: 0 1px 2px rgba(0,0,0,.15);
        }

        .login-form {
            margin-left: 65px;
        }

        legend {
            margin-right: -50px;
            font-weight: bold;
            color: #404040;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">
            <div class="row">
                <div class="login-form">
                    <h2>Login</h2>
                    <form id="loginForm" method="post" autocomplete="off" >
                        <fieldset>
                            <div id="alertInfo" class="alert alert-error clearfix" style="margin-bottom: 5px;width: 195px; padding: 2px 15px 2px 10px;display: none;">
                                           The username or password you entered is incorrect.
                            </div>
                            <div class="clearfix">
                                <input type="text" placeholder="用户名" name="username" autofocus="autofocus">
                            </div>
                            <div class="clearfix">
                                <input type="password" placeholder="密码" name="password">
                            </div>
                            <button id="loginBtn" class="btn btn-primary" type="button">Sign in</button>
                            <button class="btn" type="reset">Reset</button>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
    $.namespace("swagger.login");
    swagger.login = function () {
        return {
            login: function() {
                $.ajax({
                    type: "POST",
                    url: "submitLogin",
                    data: $("#loginForm").serialize(),
                    success: function(data) {
                        data = $.parseJSON(data);
                        if(data && "success" == data.resultCode){
                            var url = getQueryString("url");
                            url = url ? url : "/doc.html";
                            location.href = ".." + url;
                        }
                        else {
                            $("#alertInfo").show();
                            $("#loginForm")[0].reset();
                            $("input[name=username]").focus();
                        }
                    },
                    dataType: "text"
                });
            },
            unamecr: function(e) {
                if(e.which == 13) { // enter key event
                    $("input[name=password]").focus();
                }
            },
            upasscr: function(e) {
                if(e.which == 13) { // enter key event
                    $("#loginBtn").click();
                }
            }
        }
    }();

    $(function() {
        $("#loginBtn").click(swagger.login.login);
        $("input[name=username]").keypress(swagger.login.unamecr);
        $("input[name=password]").keypress(swagger.login.upasscr);
    });

    function getQueryString(name) {
        var search = window.location.search;
        if (search) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var results = search.substr(1).match(reg);
            if (results && results.length > 2) {
                return unescape(results[2]);
            }
        }

        return "";
    }

    function getContextPath() {
        var pathname = document.location.pathname;
        var index = pathname.substr(1).indexOf("/");
        var result = pathname.substr(0, index + 1);
        return result;
    }
    </script>
</body>
</html>